package com.woniuxy.springbootpro.controller;

import com.woniuxy.springbootpro.entity.Role;
import com.woniuxy.springbootpro.service.RoleService;
import com.woniuxy.springbootpro.utile.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("角色控制器")
@Controller
@RequestMapping("/role")
public class RoleController {

    private RoleService roleService;

    @Autowired
    public void setRoleService(RoleService roleService) {
        this.roleService = roleService;
    }

    @ApiOperation("查询所有角色")
    @ResponseBody
    @GetMapping("/findAllRole")
    public Result findAll(){
        List<Role> list = roleService.findAll();
        if(list != null){
            return Result.ok().data("roleList",list);
        }else {
            return Result.error();
        }
    }

    @ApiOperation("添加一位角色")
    @ResponseBody
    @PostMapping("/addRole")
    public Result add(Role role){
        return roleService.add(role)?Result.ok():Result.error();
    }

    @ApiOperation("删除角色")
    @ResponseBody
    @GetMapping("/deleteRole/{id}")
    public Result delete(@PathVariable("id") Integer id){
        return roleService.delete(id)?Result.ok():Result.error();
    }

    @ApiOperation("修改角色")
    @ResponseBody
    @PostMapping("/updataRole")
    public Result updata(Role role){
        return roleService.updata(role)?Result.ok():Result.error();
    }
}
